package com.btcode.orm.model.generater;

import com.btcode.db.executer.IDataExecuter;
import com.btcode.db.fieldtype.FieldType;
import com.btcode.db.result.Row;
import com.btcode.orm.conver.DefaultModelConver;
import com.btcode.orm.conver.IModelConver;
import java.util.List;

public class PostgreSQLGenerater extends ModelGenerater {


    /**
     * @param executer
     */
    public PostgreSQLGenerater(IDataExecuter executer) {
        super(executer);
    }

    /* (non-Javadoc)
     * @see com.bandit.orm.model.generater.ModelGenerater#getTableInfoModels()
     */
    @Override
    public List<TableInfoModel> getTableInfoModels(String tableName) {

        tableName = tableName.toLowerCase();

        String sql =
            "SELECT col.column_name,col.data_type,pk.iskey is_key FROM information_schema.columns COL"
                + "	left join ("
                + "		select pg_attribute.attname as column_name,'true' iskey,pg_class.relname tablename"
                + "			from pg_constraint  "
                + "				inner join pg_class on pg_constraint.conrelid = pg_class.oid "
                + "				inner join pg_attribute on pg_attribute.attrelid = pg_class.oid and  pg_attribute.attnum = pg_constraint.conkey[1]"
                + "				inner join pg_type on pg_type.oid = pg_attribute.atttypid"
                + "					where pg_constraint.contype='p' "
                + "		) pk on pk.tablename = col.TABLE_NAME and pk.column_name = col.column_name"
                + " where TABLE_NAME = '" + tableName + "'";

        List<Row> cloumnss = executer.queryList(sql, null);
        IModelConver modelConver = new DefaultModelConver();
        List<TableInfoModel> tableInfoModels = modelConver.converToORMModels(cloumnss,
                                                                             TableInfoModel.class);

        return tableInfoModels;
    }

    public FieldType getFieldEmunType(String type) {

        type = type.toLowerCase();

        if ("character varying".equals(type) || "character".equals(type)) {
            return FieldType.String;
        }
        else if ("date".equals(type)) {
            return FieldType.Date;
        }
        else if ("time with time zone".equals(type)) {
            return FieldType.DateTime;
        }
        else if ("timestamp with time zone".equals(type)) {
            return FieldType.DateTime;
        }
        else if ("numeric".equals(type)) {
            return FieldType.Double;
        }
        else if("double precision".equals(type)){
            return FieldType.Double;
        }
        else {
            return FieldType.String;
        }

    }

    public String getFieldJavaType(String type) {

        type = type.toLowerCase();

        if ("character varying".equals(type) || "character".equals(type)) {
            return "String";
        }
        else if ("date".equals(type)) {
            return "Date";
        }
        else if ("time with time zone".equals(type)) {
            return "Date";
        }
        else if ("timestamp with time zone".equals(type)) {
            return "Date";
        }
        else if ("numeric".equals(type)) {
            return "Double";
        }
        else if("double precision".equals(type)){
            return "Double";
        }
        else {
            return "String";
        }
    }

}
